home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtogem.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  3.9 KB  |  165 lines

  1. /* pbmtogem.c - read a portable bitmap and produce a GEM .img file
  2. **
  3. ** Author: David Beckemeyer (bdt!david)
  4. **
  5. ** Much of the code for this program was taken from other
  6. ** pbmto* programs.  I just modified the code to produce
  7. ** a .img header and generate .img "Bit Strings".
  8. **
  9. ** Thanks to Diomidis D. Spinellis for the .img header format.
  10. **
  11. ** Copyright (C) 1988 by David Beckemeyer (bdt!david) and Jef Poskanzer.
  12. **
  13. ** Permission to use, copy, modify, and distribute this software and its
  14. ** documentation for any purpose and without fee is hereby granted, provided
  15. ** that the above copyright notice appear in all copies and that both that
  16. ** copyright notice and this permission notice appear in supporting
  17. ** documentation.  This software is provided "as is" without express or
  18. ** implied warranty.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include "pbm.h"
  23.  
  24. /*
  25.  * File header structure
  26.  */
  27. struct header {
  28.     short           version;/* Image file version */
  29.     unsigned short  hlen;    /* Header length in bytes */
  30.     unsigned short  planes;    /* Number of planes */
  31.     unsigned short  patlen;    /* Pattern definition length (bytes) */
  32.     unsigned short  pxlen;    /* Pixel height (microns) */
  33.     unsigned short  pxht;    /* Pixel height (microns) */
  34.     unsigned short  linewid;/* Scan line width (pixels) */
  35.     unsigned short  nitems;    /* Number of scan line items */
  36. };
  37.  
  38. #define MAXCOL 60
  39. static unsigned short outrow[MAXCOL];
  40.  
  41. static void putinit ARGS(( struct header* hdr ));
  42. static void putbit ARGS(( bit b ));
  43. static void putrest ARGS(( void ));
  44. static void putitem ARGS(( void ));
  45. static void putrow ARGS(( void ));
  46.  
  47. void
  48. main( argc, argv )
  49.     int argc;
  50.     char* argv[];
  51.     {
  52.     FILE* ifp;
  53.     bit* bitrow;
  54.     register bit* bP;
  55.     int rows, cols, format, row, col, pad;
  56.     struct header hd;
  57.  
  58.     pbm_init( &argc, argv );
  59.  
  60.     if ( argc > 2 )
  61.     pm_usage( "[pbmfile]" );
  62.  
  63.     if ( argc == 2 )
  64.     ifp = pm_openr( argv[1] );
  65.     else
  66.     ifp = stdin;
  67.  
  68.     pbm_readpbminit( ifp, &cols, &rows, &format );
  69.     
  70.     if (cols > MAXCOL * 16)
  71.     cols = MAXCOL * 16;
  72.  
  73.     bitrow = pbm_allocrow( cols );
  74.     
  75.     hd.version = 1;    /* Image file version */
  76.     hd.hlen = 16;    /* Header length in bytes */
  77.     hd.planes = 1;    /* Number of planes */
  78.     hd.patlen = 2;    /* Pattern definition length (bytes) */
  79.     hd.pxlen = 372;    /* Pixel height (microns) */
  80.     hd.pxht = 372;    /* Pixel height (microns) */
  81.     hd.linewid = ((cols + 15) / 16) * 16;    /* Scan line width (pixels) */
  82.     hd.nitems = rows;    /* Number of scan line items */
  83.  
  84.     pad = hd.linewid - cols;
  85.  
  86.     putinit( &hd );
  87.     for ( row = 0; row < rows; ++row )
  88.     {
  89.     pbm_readpbmrow( ifp, bitrow, cols, format );
  90.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  91.         putbit( *bP );
  92.         for ( col = 0; col < pad; ++col )
  93.         putbit( 0 );
  94.         }
  95.  
  96.     pm_close( ifp );
  97.  
  98.     putrest( );
  99.  
  100.     pm_close (stdout);
  101.  
  102.     exit( 0 );
  103.     }
  104.  
  105. static short item, outcol, outmax;
  106. static short bitsperitem, bitshift;
  107.  
  108. static void
  109. putinit( hdr )
  110.     struct header* hdr;
  111.     {
  112.     fwrite( hdr, 32, 1, stdout );
  113.     item = 0;
  114.     bitsperitem = 0;
  115.     bitshift = 15;
  116.     outcol = 0;
  117.     outmax = hdr->linewid / 16;
  118.     }
  119.  
  120. #if __STDC__
  121. static void
  122. putbit( bit b )
  123. #else /*__STDC__*/
  124. static void
  125. putbit( b )
  126.     bit b;
  127. #endif /*__STDC__*/
  128.     {
  129.     if ( bitsperitem == 16 )
  130.     putitem( );
  131.     ++bitsperitem;
  132.     if ( b == PBM_BLACK )
  133.     item += 1 << bitshift;
  134.     --bitshift;
  135.     }
  136.  
  137. static void
  138. putrest( )
  139.     {
  140.     if ( bitsperitem > 0 )
  141.     putitem( );
  142.     if ( outcol > 0 )
  143.     putrow( );
  144.     }
  145.  
  146. static void
  147. putitem( )
  148.     {
  149.     outrow[outcol++] = item;
  150.     if (outcol >= outmax)
  151.     putrow( );
  152.     item = 0;
  153.     bitsperitem = 0;
  154.     bitshift = 15;
  155.     }
  156.  
  157. static void
  158. putrow( )
  159.     {
  160.     (void) putc(0x80, stdout);        /* a Bit string */
  161.     (void) putc(outcol*2, stdout);    /* count */
  162.     fwrite( outrow, outcol*2, 1, stdout );
  163.     outcol = 0;
  164.     }        
  165.